import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
	state: { //公用数据
		num: 10,
		txt: "你好",
		arr: [
			"ewrwe",
			"ret"
		],
		obj: {
			id: 1,
			name: "erfjeirj1"
		},
		homedata: []
	},
	mutations: { //mjuːˈteɪʃn 突变  	// 同步	
		fixnum(state) {
			state.num++
		},
		fixtxt(state, v) {
			state.txt = v
		},
		fixhomedata(state, v) {
			state.homedata = v
		}
	},
	actions: { //异步请求  修改还是需要同步
		queryData(store, p) {
			return new Promise(function(resolve, reject) {
				axios.get("http://www.tianqiapi.com/api?version=v9&appid=23035354&appsecret=8YvlPNrz").then(res => { //也可写其他接口来跨域
					store.commit("fixhomedata", res.data.data)					
					resolve()
				}).catch(err => {
					reject()
				})
			})
		}
	},
	modules: { //模块化

	},
	getters: { //getters类似于computed 有返回值
		www(state) {
			return state.txt + "00000000"
		}
	}
})
